package com.finance.scheduler.job;

import com.finance.framework.qurtz.TaskDescriptor;
import com.finance.scheduler.service.AbstractTask;
import com.finance.scheduler.service.LogisticsDataPuller;
import com.finance.system.service.IConfigService;
import org.apache.commons.lang.StringUtils;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Calendar;

/**
 * 拉取运单信息
 */
@Component
@DisallowConcurrentExecution
public class PullDataJob implements AbstractTask{
    protected final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LogisticsDataPuller logisticsDataPuller;
    @Autowired
    private IConfigService configService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
        int before = -1;
        String pull_offset_day = this.configService.findConfig("pull_offset_day");
        if(StringUtils.isNotBlank(pull_offset_day)){
            before = Integer.valueOf(pull_offset_day);
        }
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.DAY_OF_MONTH,before);
        this.logisticsDataPuller.pullData(calendar);
    }

    @Override
    public TaskDescriptor descriptor() {
        return new TaskDescriptor("N日前完结运单获取","pullData","pullDataTrigger","pullData","0/5 * * ? * *",this.getClass().getCanonicalName(),"拉取运单数据");
    }
}
